################################################################################
#REPLICATION PACKAGE FOR: ######################################################
#THE REPRESENTATIONAL CONSEQUENCES OF MUNICIPAL CIVIL SERVICE REFORM############
#AMERICAN POLITICAL SCIENCE REVIEW##############################################
#NICHOLAS KUIPERS AND ALEXANDER SAHN############################################
#MARCH 15, 2022#################################################################
################################################################################


rm(list=ls())
setwd("~/Dropbox/merit_project")
#setwd('/Users/alexandersahn/Work Dropbox/Dropbox (Princeton)/merit_project')

#REPLICATORS: SET YOUR WORKING DIRECTORY HERE

library(rdrobust)
library(tidyverse)
library(ipumsr)
library(labelled)
library(readstata13)
library(estimatr)
library(ggrepel)
library(lfe)
library(stargazer)
library(fixest)
library(rdrobust)
library(purrr)


#(1) data prep and wrangling-------------------------------------------------------------------------------

#load the functions you will need to conduct the analyses/clean the data
source("./replication_file/_6_functions.R")

#make the aggregated data frame, drawing on IPUMS (N.B. replicators cannot run this script, 
#as we are not permitted by IPUMS to provide the underlying full count census data. If you wish to run this script, please contact
#the authors for the full details of the IPUMS extracts you will need to collect.)
#source("./replication_file/_3_code/1_make_census_long.R")

#load the aggregated data frame that is returned by the script "make_census_long.R"
analysis_df = read.csv("./replication_file/_4_data/census_stack_long_new_bc.csv")

#reshape the aggregated data frame into analysis data frames
source("./replication_file/_3_code/1_make_analysis_data.R")

keep_objs = lsf.str() %>% as.vector() #naming the objects we want to keep in the environment throughout
keep_objs = append(keep_objs, c("keep_objs", "analysis_df", "analysis_df_1", "analysis_df_2", "analysis_df_3", "analysis_df_4", "analysis_df_5", "analysis_df_6"))

#(2) Main paper analyses-------------------------------------------------------------------------------

#figure 1 --- city size scatter plot
source("./replication_file/_3_code/2_figure_1.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

#figure 2 -- reforms over the years
source("./replication_file/_3_code/2_figure_2.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

#figures 3 + 4 -- event study analyses
source("./replication_file/_3_code/2_figure_3_4.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

#table 1 -- iv analysis
source("./replication_file/_3_code/2_table_1.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

#figure 5 -- rdd analyses
source("./replication_file/_3_code/2_figure_5.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

#figure 6 -- city size event study analyses
source("./replication_file/_3_code/2_figure_6.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

#figure 7 -- literacy event study analyses
source("./replication_file/_3_code/2_figure_7.R")
rm(list=setdiff(ls(), keep_objs)) #clean up


#(3) Appendix analyses-------------------------------------------------------------------------------

#figure A1 -- descriptive scatter plots 
source("./replication_file/_3_code/3_figure_a1.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

#figure A2 -- descriptive trends in irish population
source("./replication_file/_3_code/3_figure_a2.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

#figure A3 -- descriptive trends in municipal job composition
source("./replication_file/_3_code/3_figure_a3.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

#Tables A2 + A3 -- see RD script (2_figure_5.R) above

#figures A4 + A5 -- event study analyses, fire and police depts
source("./replication_file/_3_code/3_figure_a4_a5.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

#figures A6 -- event study analyses, by share of population that was foreign born
source("./replication_file/_3_code/3_figure_a6.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

#figures A7 -- event study analyses, by population size
source("./replication_file/_3_code/3_figure_a7.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

#figures A8 -- event study analyses, by machine dominance
source("./replication_file/_3_code/3_figure_a8.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

#figures A9 + A10 -- event study analyses, by region
source("./replication_file/_3_code/3_figure_a9_a10.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

#figure A11 -- event study analyses, by literacy
source("./replication_file/_3_code/3_figure_a11.R")
rm(list=setdiff(ls(), keep_objs)) #clean up

#Table A4  -- lagged dv analysis
source("./replication_file/_3_code/3_table_a4.R")
rm(list=setdiff(ls(), keep_objs)) #clean up
